package cn.com.shadowless.xinning.view.fragment;

import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mengpeng.mphelper.ToastUtils;

import butterknife.BindView;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.order.AssetMoveInfo;
import cn.com.shadowless.xinning.presenter.OrderPresenter;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseFragment;

/**
 * The type Asset move fragment.
 */
public class AssetMoveFragment extends BaseFragment {

    /**
     * The Move order no.
     */
    @BindView(R.id.move_orderNo)
    TextView moveOrderNo;
    /**
     * The Move make time.
     */
    @BindView(R.id.move_makeTime)
    TextView moveMakeTime;
    /**
     * The Move maker.
     */
    @BindView(R.id.move_maker)
    TextView moveMaker;
    /**
     * The Move make position.
     */
    @BindView(R.id.move_makePosition)
    TextView moveMakePosition;
    /**
     * The Move asset id.
     */
    @BindView(R.id.move_assetId)
    TextView moveAssetId;
    /**
     * The Move asset name.
     */
    @BindView(R.id.move_assetName)
    TextView moveAssetName;
    /**
     * The Move asset model.
     */
    @BindView(R.id.move_assetModel)
    TextView moveAssetModel;
    /**
     * The Move buy date.
     */
    @BindView(R.id.move_buyDate)
    TextView moveBuyDate;
    /**
     * The Move buy in value.
     */
    @BindView(R.id.move_buyInValue)
    TextView moveBuyInValue;
    /**
     * The Move cut limit year.
     */
    @BindView(R.id.move_cutLimitYear)
    TextView moveCutLimitYear;
    /**
     * The Move up cut cost.
     */
    @BindView(R.id.move_upCutCost)
    TextView moveUpCutCost;
    /**
     * The Move book value.
     */
    @BindView(R.id.move_bookValue)
    TextView moveBookValue;
    /**
     * The Move belong machine.
     */
    @BindView(R.id.move_belongMachine)
    TextView moveBelongMachine;
    /**
     * The Move random data.
     */
    @BindView(R.id.move_randomData)
    TextView moveRandomData;
    /**
     * The Move last move use.
     */
    @BindView(R.id.move_lastMoveUse)
    TextView moveLastMoveUse;
    /**
     * The Move move depart.
     */
    @BindView(R.id.move_moveDepart)
    TextView moveMoveDepart;
    /**
     * The Move out storage local.
     */
    @BindView(R.id.move_outStorageLocal)
    TextView moveOutStorageLocal;
    /**
     * The Move move out duty.
     */
    @BindView(R.id.move_moveOutDuty)
    TextView moveMoveOutDuty;
    /**
     * The Move move out use.
     */
    @BindView(R.id.move_moveOutUse)
    TextView moveMoveOutUse;
    /**
     * The Move move in depart.
     */
    @BindView(R.id.move_moveInDepart)
    TextView moveMoveInDepart;
    /**
     * The Move in storage local.
     */
    @BindView(R.id.move_inStorageLocal)
    TextView moveInStorageLocal;
    /**
     * The Move move in duty.
     */
    @BindView(R.id.move_moveInDuty)
    TextView moveMoveInDuty;
    /**
     * The Move check process.
     */
    @BindView(R.id.move_check_process)
    LinearLayout moveCheckProcess;

    private final String url = "api/Allot/LoadCheckHistory?%s=";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_asset_move_layout;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            String orderId = bundle.getString("orderId");
            String userName = bundle.getString("userName");
            initMoveInfo(orderId, userName);
            initCheckHistoryList(String.format(url, "id") + orderId, mActivity, moveCheckProcess);
        }
    }

    private void initMoveInfo(String orderId, String userName) {
        OrderPresenter
                .getSingleton()
                .getAssetMoveInfo(orderId, userName, new RequestCallBack<AssetMoveInfo>() {
                    @Override
                    public void success(RequestResult<AssetMoveInfo> result) {
                        String token = result.getLoginToken();
                        String oldToken = PreferencesUtils.getString(MyApplication.getInstance(), "token", "-1");
                        if (result.isSucceed() && null != result.getResult()) {
                            if (null != token && token.equals(oldToken)) {
                                Log.e(TAG, "success: " + result.toString());
                                AssetMoveInfo.DataBean dataBean = result.getResult().getData();
                                AssetMoveInfo.DataBean.AssetBean assetBean = dataBean.getAsset();

                                String id = dataBean.getId();
                                String createTimeStr = dataBean.getCreateTimeStr();
                                String createUserName = dataBean.getCreateUserName();
                                moveOrderNo.setText(id);
                                moveMakeTime.setText(createTimeStr);
                                moveMaker.setText(createUserName);

                                String assetNumber = assetBean.getAssetNumber();
                                String assetName = assetBean.getAssetName();
                                String modelNumber = assetBean.getModelNumber();
                                String startDateStr = assetBean.getStartDateStr();
                                String originalValue = getDecimal(assetBean.getOriginalValue().doubleValue());
                                String depreciationYear = getDecimal(dataBean.getDepreciationYear().doubleValue());
                                String depreciationValue = getDecimal(dataBean.getDepreciationValue().doubleValue());
                                String leftValue = getDecimal(dataBean.getLeftValue().doubleValue());
                                String affiliates = dataBean.getAffiliates();
                                String docu = dataBean.getDocu();
                                String beforeAllot = dataBean.getBeforeAllot();
                                String outDepartName = dataBean.getOutDepartName();
                                String outPlace = dataBean.getOutPlace();
                                String outDepartOwnerName = dataBean.getOutDepartOwnerName();
                                String afterAllot = dataBean.getAfterAllot();
                                String inDepartName = dataBean.getInDepartName();
                                String inPlace = dataBean.getInPlace();
                                String inDepartOwnerName = dataBean.getInDepartOwnerName();
                                moveAssetId.setText(assetNumber);
                                moveAssetName.setText(assetName);
                                moveAssetModel.setText(modelNumber);
                                moveBuyDate.setText(startDateStr);
                                moveBuyInValue.setText(originalValue);
                                moveCutLimitYear.setText(depreciationYear);
                                moveUpCutCost.setText(depreciationValue);
                                moveBookValue.setText(leftValue);
                                moveBelongMachine.setText(affiliates);
                                moveRandomData.setText(docu);
                                moveLastMoveUse.setText(beforeAllot);
                                moveMoveDepart.setText(outDepartName);
                                moveOutStorageLocal.setText(outPlace);
                                moveMoveOutDuty.setText(outDepartOwnerName);
                                moveMoveOutUse.setText(afterAllot);
                                moveMoveInDepart.setText(inDepartName);
                                moveInStorageLocal.setText(inPlace);
                                moveMoveInDuty.setText(inDepartOwnerName);
                            } else {
                                ToastUtils.onErrorShowToast("登录态与服务器不一致");
                                initLogoutInfo(mActivity, userName);
                            }
                        } else {
                            ToastUtils.onErrorShowToast("该订单暂无详细信息");
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                });
    }

    @Override
    protected void initView() {

    }
}
